﻿using System;
using System.Windows.Forms;
using GrapeCity.ActiveReports.Samples.EndUserDesigner;
using GrapeCity.ActiveReports.Design;

namespace ActiveReportsDemo.Viewers
{
    public partial class DemoBaseUserControl : UserControl
    {
        /// <summary>
        /// 报表浏览器
        /// </summary>
        public GrapeCity.ActiveReports.Viewer.Win.Viewer ReportViewer
        {
            get 
            {
                return this.viewer1; 
            }
            set
            {
                this.viewer1 = value; 
            }
        }

        /// <summary>
        /// 报表浏览器
        /// </summary>
        public System.Windows.Forms.WebBrowser ReportWebBrowser
        {
            get
            {
                return this.webBrowser1;
            }
            set
            {
                this.webBrowser1 = value;
            }
        }

        /// <summary>
        /// 当前Viewer控件中显示报表的名称
        /// </summary>
        public string CurrentReportName
        { get; set; }

        /// <summary>
        /// 当前Viewer控件中显示报表的ID
        /// </summary>
        public string CurrentReportID
        { get; set; }

        /// <summary>
        /// 修改当前报表所使用的皮肤
        /// </summary>
        public virtual void ChangeTheme()
        {

        }

        /// <summary>
        /// 显示报表
        /// </summary>
        public virtual void ShowReport()
        {
            viewer1 = new GrapeCity.ActiveReports.Viewer.Win.Viewer();
            webBrowser1 = new WebBrowser();
        }

        public DemoBaseUserControl()
        {
            InitializeComponent();

            CustomizeToolBar();
        }

        #region 工具栏自定义

        // 导出按钮
        public  ToolStripButton _exportButton;

        // 设计按钮
        public  ToolStripButton _designButton;

        // 报表设计器
        public Designer reportDesigner = new Designer();

        /// <summary>
        /// 自定义工具栏，添加报表导出和自定义按钮
        /// </summary>
        private void CustomizeToolBar()
        {            
            viewer1.LoadCompleted += new GrapeCity.ActiveReports.Document.LoadCompletedEventHandler(viewer1_LoadCompleted);

            this._exportButton = new ToolStripButton();
            this._designButton = new ToolStripButton();
            this._designButton.Name = "Design";
            this._designButton.Text = "设计";
            this._designButton.Enabled = false;
            this._designButton.ToolTipText = "修改报表模板";
            this._designButton.Image = ActiveReportsDemo.Properties.Resources.Designer;
            this._exportButton.Name = "Export";
            this._exportButton.Text = "导出";
            this._exportButton.Enabled = false;
            this._exportButton.ToolTipText = "导出报表";
            this._exportButton.Image = ActiveReportsDemo.Properties.Resources.Export;

            this.viewer1.Toolbar.ToolStrip.Items.Insert(3,_exportButton);
            this.viewer1.Toolbar.ToolStrip.Items.Insert(4,_designButton);

            _exportButton.Click += new EventHandler(_exportButton_Click);
            _designButton.Click += new EventHandler(_designButton_Click);
            
        }

        void viewer1_LoadCompleted(object sender, EventArgs e)
        {
           this. _designButton.Enabled = true;
           this. _exportButton.Enabled = true;
        }

        private void _exportButton_Click(object sender, EventArgs e)
        {
            DesignerReportType reporttype = DesignerReportType.Section;
 
            if (CurrentReportName.IndexOf(".rpx") != -1)
            {
                reporttype = DesignerReportType.Section;
            }
            else if (CurrentReportName.IndexOf(".rdlx") != -1)
            {
                reporttype = DesignerReportType.Page;
            }

            ExportForm exportForm = new ExportForm(reporttype, reportDesigner.Report, ReportViewer);
            exportForm.ShowDialog();
        }

        private void _designButton_Click(object sender, EventArgs e)
        {
            EndUserDesigner designForm;
            designForm = new EndUserDesigner();
            designForm.CurrentReport = CurrentReportName;
            
            if (designForm.ShowDialog() == DialogResult.OK)
            {
                ShowReport();
            }
        }

        #endregion

        private void picDownLoad_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.gcpowertools.com.cn/products/download.aspx?pid=16&from=ARDemo");
        }
    }
}
